package com.bwie.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.Delete;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupActive;
import com.bwie.mapper.GroupActiveMapper;
import com.bwie.service.GroupActiveService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 鑫仔
 * @Date: 2025/7/26 11:25
 * @Description:
 **/
@Transactional
@Service
public class GroupActiveServiceImpl extends ServiceImpl<GroupActiveMapper, GroupActive> implements GroupActiveService {

    @Autowired
    private GroupActiveMapper groupActiveMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R groupActiveList(PageInfoVo pageInfoVo) {
        Page<GroupActive> activePage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        LambdaQueryWrapper<GroupActive> wrapper = new LambdaQueryWrapper<>();
        if (pageInfoVo.getKeyWord() != null) {
            wrapper.like(GroupActive::getActiveName, pageInfoVo.getKeyWord());
        }
        wrapper.orderByDesc(GroupActive::getActiveId);
        Page<GroupActive> page = groupActiveMapper.selectPage(activePage, wrapper);
        return R.OK(page);
    }

    @Override
    public R addGroupActive(GroupActive groupActive) {
        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);

        GroupActive groupActiveDb = groupActiveMapper.selectOne(
                new LambdaQueryWrapper<GroupActive>().eq(GroupActive::getActiveName, groupActive.getActiveName())
        );
        if (ObjectUtil.isNotEmpty(groupActiveDb)) {
            return R.ERROR(500, "活动不可重复添加");
        }

        groupActive.setCreateBy(userName);
        groupActive.setCreateTime(new Date());
        groupActiveMapper.insert(groupActive);
        return R.OK();
    }

    @Override
    public R deleteGroupActive(Integer activeId) {
        GroupActive groupActive = groupActiveMapper.selectById(activeId);
        if (ObjectUtils.isEmpty(groupActive)) {
            return R.ERROR(500, "活动不存在");
        }
        groupActiveMapper.deleteById(activeId);
        return R.OK();
    }

    @Override
    public R updateGroupActive(GroupActive groupActive) {
        GroupActive groupActiveDb = groupActiveMapper.selectOne(
                new LambdaQueryWrapper<GroupActive>().eq(GroupActive::getActiveId, groupActive.getActiveId())
        );
        if (ObjectUtils.isEmpty(groupActiveDb)) {
            return R.ERROR(500, "活动不存在");
        }
        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);

        groupActive.setUpdateBy(userName);
        groupActive.setUpdateTime(new Date());
        groupActiveMapper.updateById(groupActive);
        return R.OK();
    }
}
